import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_project01/apis/home.dart';
import 'package:flutter_project01/pages/home/components/home_list.dart';
import 'package:flutter_project01/pages/home/components/home_nav.dart';
import 'package:flutter_project01/utils/TokenManager.dart';
import 'package:flutter_project01/utils/toast.dart';
import 'package:fluttertoast/fluttertoast.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  Timer? timer;
  int countDown = 10;
  List announcementList = [];
  @override
  void initState() {
    super.initState();
    getAnnouncementList();
  }

  getAnnouncementList() async {
    final res = await HomeApi.getAnnouncementList();
    // PromptAction.showSuccess(res.toString());
    setState(() {
      announcementList = res;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffd5e3f2),
      appBar: AppBar(
        backgroundColor: const Color(0xffd5e3f2),
        centerTitle: true,
        title: const Text('享+社区'),
      ),
      body: ListView(
        children: [
          HomeNav(),
          // TextButton(
          //     onPressed: () async {
          //       PromptAction.showSuccess('成功');
          //     },
          //     child: Text('弹窗')),
          // TextButton(
          //     onPressed: () async {
          //       final res = await HomeApi.getAnnouncementList();
          //       print(res);
          //     },
          //     child: Text('测试')),
          // Banner
          Container(
            margin: EdgeInsets.symmetric(horizontal: 20, vertical: 16),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(10),
              child: Image.asset(
                'assets/images/banner@2x.jpg',
                fit: BoxFit.cover,
                height: 120,
                width: double.infinity,
              ),
            ),
          ),
          // 社区公告
          HomeList(notifyList: announcementList),
          Container(
            child: Row(
              children: [
                TextButton(
                  onPressed: () {
                    showDialog(
                        context: context,
                        builder: (con) {
                          return AlertDialog(
                              title: Text('测试弹窗'),
                              content: Text('测试弹窗内容'),
                              actions: [
                                TextButton(
                                    onPressed: () {
                                      Navigator.of(context).pop();
                                    },
                                    child: Text('取消')),
                                TextButton(onPressed: () {}, child: Text('确定')),
                              ]);
                        });
                  },
                  child: Text('弹窗'),
                ),
                TextButton(
                  onPressed: () {
                    showBottomSheet(
                        context: context,
                        builder: (context) {
                          return Container(
                            height: 200,
                            width: double.infinity,
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.only(
                                topLeft: Radius.circular(10),
                                topRight: Radius.circular(10),
                              ),
                            ),
                            child: Text('测试弹窗内容'),
                          );
                        });
                  },
                  child: Text('半模态'),
                ),
                TextButton(
                  onPressed: () {
                    showDatePicker(
                        context: context,
                        firstDate: DateTime(2025),
                        lastDate: DateTime(2026),
                        initialDate: DateTime.now());
                  },
                  child: Text('日期选择'),
                )
              ],
            ),
          ),
          Container(
            child: Wrap(
              children: [
                TextButton(
                  onPressed: () {
                    tokenManager.setToken('1234567890','dadfaaf');
                  },
                  child: const Text('持久化存储-存'),
                ),
                TextButton(
                  onPressed: () async {
                    final token = await tokenManager.getToken();
                    PromptAction.showSuccess(token);
                  },
                  child: const Text('持久化存储-取'),
                ),
                TextButton(
                  onPressed: () {
                    tokenManager.deleteToken();
                  },
                  child: const Text('持久化存储-删'),
                ),
                TextButton(
                  onPressed: () {
                    Navigator.pushNamed(context, '/login');
                  },
                  child: const Text('跳转登录页'),
                ),
                TextButton(
                  onPressed: () {
                    Navigator.pushNamed(context, '/profile');
                  },
                  child: const Text('跳转个人信息页'),
                ),
              ],
            ),
          ),
          Container(
            child: Row(
              children: [
                // 开启定时器
                TextButton(
                  onPressed: () {
                    // 开启定时器
                    timer = Timer.periodic(const Duration(seconds: 1), (timer) {
                      setState(() {
                        countDown--;
                        if (countDown <= 0) {
                          timer.cancel();
                          countDown = 10;
                        }
                      });
                    });
                  },
                  child: const Text('开启定时器'),
                ),
                Text('秒数$countDown'),
                // 定时器关闭
                TextButton(
                  onPressed: () {
                    // 关闭定时器
                    timer?.cancel();
                  },
                  child: const Text('关闭定时器'),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}
